package com.gxuwz.prrs.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.gxuwz.prrs.R;
import com.gxuwz.prrs.bean.Notice;

import java.util.List;

/**
 * @author 张钧
 * @Description
 * @create 2023-05-17 下午 09:46
 */
public class NoticeAdapter extends UpdatableAbstractAdapter<Notice> {

	private final List<Notice> notices;
	private final LayoutInflater inflater;

	public NoticeAdapter(Context context, List<Notice> notices) {
		this.notices = notices;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return notices.size();
	}

	@Override
	public Notice getItem(int i) {
		return notices.get(i);
	}

	@Override
	public long getItemId(int i) {
		return notices.get(i).getNoticeId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		NoticesViewHolder viewHolder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.notice_item_layout, parent, false);
			viewHolder = new NoticesViewHolder();
			viewHolder.setTitle(convertView.findViewById(R.id.title));
			viewHolder.setContent(convertView.findViewById(R.id.content));
			viewHolder.setNoticeTime(convertView.findViewById(R.id.notetime));
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (NoticesViewHolder) convertView.getTag();
		}
		Notice itemData = getItem(position);
		viewHolder.getTitle().setText(itemData.getTitle());
		viewHolder.getContent().setText(itemData.getContent());
		viewHolder.getNoticeTime().setText(itemData.getNoticeTime());
		return convertView;
	}

	@Override
	public void updateData(List<Notice> newData) {
		if(newData != null){
			notices.clear();
			notices.addAll(newData);
			notifyDataSetChanged();
		}
	}
}

class NoticesViewHolder {
	private TextView title;
	private TextView content;
	private TextView noticeTime;

	public TextView getNoticeTime() {
		return noticeTime;
	}

	public void setNoticeTime(TextView noticeTime) {
		this.noticeTime = noticeTime;
	}

	public TextView getTitle() {
		return title;
	}

	public void setTitle(TextView title) {
		this.title = title;
	}

	public TextView getContent() {
		return content;
	}

	public void setContent(TextView content) {
		this.content = content;
	}

}
